﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace FPC.Offer.IRepository
{
    /// <summary>
    ///仓储接口
    /// </summary>
    /// <typeparam name="T">定义泛型，约束其是一个类</typeparam>
    public interface IRepository<T> where T : class
    {
        /// <summary>
        /// 根据过滤条件，获取记录
        /// </summary>
        /// <param name="exp">lambda 表达式条件 The exp.</param>
        IQueryable<T> Find(Expression<Func<T, bool>> exp = null);

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="exp">lambda 表达式条件</param>
        /// <returns></returns>
        bool IsExist(Expression<Func<T, bool>> exp);

        /// <summary>
        /// 查找单条数据
        /// </summary>
        /// <param name="exp">lambda 表达式条件</param>
        /// <returns></returns>
        T FindSingle(Expression<Func<T, bool>> exp);

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        List<T> Select();

        /// <summary>
        /// 通过条件查询满足所有
        /// </summary>
        /// <param name="exp">lambda 表达式条件</param>
        /// <returns></returns>
        List<T> Select(Expression<Func<T, bool>> exp);

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="totalSize">总条数</param>
        /// <returns></returns>
        List<T> SelectPage(int pageIndex, int pageSize, out long totalSize);

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="exp">lambda 表达式条件</param>
        /// <param name="totalSize">总条数</param>
        /// <returns></returns>
        List<T> SelectPage(int pageIndex, int pageSize, Expression<Func<T, bool>> exp, out long totalSize);

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">页序号（从1开始）</param>
        /// <param name="pageSize">每页条数</param>
        /// <param name="exp">lambda 表达式Where条件</param>
        /// <param name="order">lambda 表达式排序条件</param>
        /// <param name="orderType">true 是正序 false 则相反</param>
        /// <param name="totalSize">总条数</param>
        /// <returns></returns>
        List<T> SelectPageOrder(int pageIndex, int pageSize, Expression<Func<T, bool>> exp, Expression<Func<T, object>> order, bool orderType, out long totalSize);

        /// <summary>
        /// 通用单实体添加
        /// </summary>
        /// <param name="entity">实体</param>
        int Add(T entity);

        /// <summary>
        /// 通用单实体批量添加
        /// </summary>
        /// <param name="entities">实体集合</param>
        int Add(List<T> entities);

        /// <summary>
        /// 通用单实体修改
        /// </summary>
        /// <param name="entity">实体</param>
        int Update(T entity);

        /// <summary>
        /// 通用单实体删除
        /// </summary>
        /// <param name="entity">实体</param>
        int Delete(T entity);

        /// <summary>
        /// DBContext上下文中的缓存与数据库进行同步保存
        /// </summary>
        int SaveChanges();
        /// <summary>
        /// 查询数据库全部表
        /// </summary>
        /// <returns></returns>

    }
}
